package com.aitoedu.server.controller.student;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.util.SaResult;
import com.aitoedu.server.dto.SubmitDTO;
import com.aitoedu.server.service.PreviewService;
import com.aitoedu.server.service.PreviewStatusService;
import com.aitoedu.server.service.ScService;
import com.aitoedu.server.service.StestbankService;
import com.aitoedu.server.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/student/sc")
@Slf4j
@CrossOrigin
@Tag(name = "学生端课程相关接口")
@SaCheckRole("学生")
public class CourseClassController {

    @Autowired
    private ScService scService;

    @Autowired
    private StestbankService stestbankService;

    @Autowired
    private PreviewService previewService;

    @Autowired
    private PreviewStatusService previewStatusService;

    @GetMapping("/join")
    @Operation(summary = "加入班级")
    public SaResult joinClass(int code){
        return scService.joinClass(code);
    }

    @GetMapping("/question")
    @Operation(summary = "学生查询对应课程的章节的所有题目")
    public SaResult question(Long chapterId){
        List<QuestionVO> questionVOList = stestbankService.getAllQuestion(chapterId);
        return SaResult.ok().setData(questionVOList);
    }

    @GetMapping("/score")
    @Operation(summary = "学生查询对应课程的章节的分数")
    public SaResult score(Long chapterId){
        int score = stestbankService.score(chapterId);
        return SaResult.ok().setData(score);
    }

    @PostMapping("/submit")
    @Operation(summary = "学生提交作业")
    public SaResult submit(@RequestBody List<SubmitDTO> submitDTOList){
        stestbankService.submit(submitDTOList);
        return SaResult.ok();
    }

    @GetMapping("/myQuestion")
    @Operation(summary = "学生查询所有课程的所有章节的题目")
    public SaResult myQuestion(){
        List<MyQuestionVO> list = stestbankService.myQuestion();
        return SaResult.ok().setData(list);
    }

    @GetMapping("/preview")
    @Operation(summary = "学生查询对应章节所有预习内容")
    public SaResult preview(Long chapterId){
        String status="1";
        List<PreviewVO> contents = previewService.preview(chapterId);
        List<PreviewVO> collect = contents.stream().filter(c -> c.getPreStatus() == 0).collect(Collectors.toList());
        if(collect != null) status = "0";
        return SaResult.ok().setData(contents).setMsg(status);
    }

    @GetMapping("/myCourse")
    @Operation(summary = "学生查询所选课程")
    public SaResult myCourse(){
        List<StuCourseVO> stuCourseVOList = scService.myCourse();
        return SaResult.ok().setData(stuCourseVOList);
    }

    @GetMapping("/previewList")
    @Operation(summary = "学生查询预习列表")
    public SaResult previewList(){
        List<CoursePreviewVO> coursePreviewVOList = previewService.previewList();
        return SaResult.ok().setData(coursePreviewVOList);
    }

    @GetMapping("/setOne")
    @Operation(summary = "将查询过后的预习信息状态置为已预习")
    public SaResult setOne(Long psId){
        previewStatusService.setOne(psId);
        return SaResult.ok();
    }
}
